import BpmInstApi from "@/api/bpm/core/bpmInst";
import SysAppApi from "@/api/system/core/sysApp";
import { mapState} from 'vuex';

export default {
    computed:{
        ...mapState({
            appKey: state => state.appSetting.appKey,
        }),
        //是否时独立打开审批页面还是使用的统一URL打开
        isAlone(){
            return this.$route.name!="openDoc" && !this.newPage;
        }
    },
    data(){
        return {
            newPage:true
        }
    },
    methods:{
        encryptInstId(instId,callback){
            BpmInstApi.getBpmInstEncrypt(instId).then(res=>{
                if(!res.success){
                    this.$message.warn(res.message);
                    return;
                }
                callback(res.data);
            })
        },
        encryptInstIds(instIds,relType,callback){
            BpmInstApi.getBpmInstEncryptList(instIds,relType).then(resultList=>{
                callback(resultList);
            })
        },
        /**
         * 启动流程页面
         * @param defKey
         */
        async startProcess(defKey,appId){
            SysAppApi.get(appId).then(res=>{
                let app=res.data;
                let path=app.path;
                let url=`${path}/${app.clientCode}/new/${defKey}?appId=${appId}`;
                window.open(url,"_blank" );
            })
        },
        /**
         * 打开流程实例页面
         * <pre>
         *     1.审批页面
         *     2.流程实例
         * </pre>
         * @param instId 示例ID
         * @param taskId 任务ID
         * @param taskType
         */
        async openInst(appId,instId,taskId){

            var self=this;
            var url=null;
            let appKey;
            if(appId && appId!='0'){
               var  res= await SysAppApi.get(appId);
                let app= res.data;
                if("1"==app.menuNavType ||"2"==app.menuNavType){
                    url=app.path;
                    appKey=app.clientCode;
                }
            }



            self.encryptInstId(instId,function(encryptInstId){
                appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                SysAppApi.get(appId).then(res=>{
                    let app=res.data;
                    let path=app.path;
                    let url=`${path}/${app.clientCode}/openDoc/${encryptInstId}?appId=${appId}`;
                    if(taskId){
                        url += "&taskId="+taskId;
                    }
                    window.open(url,"_blank" );
                })
            })
        },
        /**
         * 打开明细
         * @param instId
         *
         */
        async openDetail(appId,instId,from){
            var self=this;
            var url=null;
            let appKey;
            if(appId && '0'!=appId){
                var  res= await SysAppApi.get(appId);
                let app= res.data;
                if("1"==app.menuNavType ||"2"==app.menuNavType){
                    url=app.path;
                    appKey=app.clientCode;
                }
            }
            self.encryptInstId(instId,function(encryptInstId){
                appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                SysAppApi.get(appId).then(res=>{
                    let app=res.data;
                    let path=app.path;
                    let url=`${path}/${app.clientCode}/openDoc/${encryptInstId}?action=detail&appId=${appId}`;
                    if(from){
                        url += "&from="+from;
                    }
                    window.open(url,"_blank" );
                })
            })
        },
        /**
         * 打开流程实例页面
         * <pre>
         *     1.审批页面
         *     2.流程实例
         * </pre>
         * @param instId 示例ID
         * @param appId 应用ID
         */
        async openOperator(record){
            var self=this;
            self.encryptInstId(record.instId,function(encryptInstId){
                let appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                SysAppApi.get(appId).then(res=>{
                    let app=res.data;
                    let path=app.path;
                    let url=`${path}/${app.clientCode}/openDoc/${encryptInstId}?operator=operator&appId=${appId}&defId=${record.defId}&instId=${record.instId}`;
                    window.open(url,"_blank" );
                })
            })
        },
        getAppKey(){
            let state=this.$store.state;
            //这个数据只在大前端有，appKey返回为空。
            if(state.appSetting.passwordmode){
                return "";
            }
            let key= state.appSetting?state.appSetting.appKey:'';
            return key;
        },
        pushInst(instId,appId){
            var self=this;
            this.encryptInstId(instId,function(encryptInstId) {
                encryptInstId=encryptInstId.instId;
                let time = new Date().getTime();
                let appKey=self.getAppKey();
                let path=appKey?"/"+ appKey:"";
                path+='/bpm/workbench/openDoc/' + encryptInstId + '?time=' + time;
                if(appId) {
                    path += "&appId=" + appId;
                }
                self.$router.push({path: path});
            });
        },
        copyInst(instId){
            let appKey=this.getAppKey();
            let params={instId:instId};
            params.appKey=appKey;
            let obj=this.$router.resolve({
                name:'openDoc',
                params:params
            });
            let url=obj.href +"?action=copy";
            window.open(url,"_blank" );
        },
        executeMethod(method,data){
            let rxForms=this.$refs.rxForms;
            if(!rxForms){
                return;
            }
            rxForms.executeMethod(method,data);
        }

    }
}